/* 
 * File:   main.c
 * Author: gustavo
 *
 * Created on August 9, 2011, 7:35 PM
 */

#include <stdio.h>
#include <stdlib.h>

#include <mpack/matrix/matrix.h>
#include <mpack/image/image.h>
#include <mpack/image/io/imageio.h>
#include <mpack/geom/primitives.h>
#include <mpack/image/drawing/imdraw.h>

void harris(void) {

	Image * iGray = NULL;
	Image * iRGB = NULL;

	Matrix * mGray = NULL;
	Matrix * mHarris = NULL;
	Matrix * mNMS = NULL;

	VertexList * VL = NULL;
	VertexList * Hull = NULL;

	iGray = image_read_pgm("images/cal.pgm");
	//iGray = image_read_pgm("images/chess.pgm");

	image_gray2mat(iGray, &mGray);
	image_features_harris(mGray, &mHarris);
	image_save_matrix_as_image(mHarris, "images/results/harris.ppm");

	image_nonmaxsup_feature(mHarris, 2, &mNMS);
	image_threshold(mNMS, 100.0, 0.0, 255.0, 0);
	image_save_matrix_as_image(mNMS, "images/results/nms.ppm");
	
	VL = imp_get_vertex_list_from_matrix(mNMS);
	imp_sort_vertex_list(VL);

	Hull = imp_get_convex_hull(VL);

	image_mat2gray(mNMS, &iGray);
	image_gray2rgb(iGray, &iRGB);	
	
	imp_draw_vertex_list(iRGB, Hull);
	image_save_ppm(iRGB, "images/results/vertex.ppm");

	image_delete(iGray);
	matrix_delete(mGray);
	matrix_delete(mHarris);
	matrix_delete(mNMS);

}

/*
 * 
 */
int main(int argc, char** argv) {

	harris();

	return (EXIT_SUCCESS);
}

